package com.xhwl.logistics.converter.mapstruct;

import com.xhwl.logistics.bo.VehicleCarBO;
import com.xhwl.logistics.bo.VehicleCardBO;
import com.xhwl.logistics.bo.VehicleRelationCarToCardBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleCarBO;
import com.xhwl.logistics.dto.manipulate.VehicleCarCreateDTO;
import com.xhwl.logistics.dto.manipulate.VehicleCarDeleteDTO;
import com.xhwl.logistics.dto.manipulate.VehicleCarUpdateDTO;
import com.xhwl.logistics.dto.query.VehicleCarDTO;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.enums.VehicleCarTypeEnum;
import com.xhwl.logistics.enums.VehicleCardTypeEnum;
import com.xhwl.logistics.po.VehicleCarPO;
import com.xhwl.logistics.po.VehicleCardPO;
import com.xhwl.logistics.po.VehicleRelationCarToCardPO;
import com.xhwl.logistics.vo.VehicleCarVO;
import com.xhwl.logistics.vo.VehicleCardVO;
import com.xhwl.logistics.vo.VehicleRelationCarToCardVO;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.NullValueMappingStrategy;

import java.util.List;
import java.util.Objects;

@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface VehicleCarSecondConverter {

    //*********Query BO <- Controller DTO*********
    @Mapping(target = "projectIdList", source = "projectIds")
    @Mapping(target = "carType", ignore = true)
    QueryVehicleCarBO vehicleCarDTO2QueryVehicleCarBO(VehicleCarDTO vehicleCarDTO);
    default QueryVehicleCarBO toQueryVehicleCarBO(VehicleCarDTO vehicleCarDTO) {
        QueryVehicleCarBO queryVehicleCarBO = vehicleCarDTO2QueryVehicleCarBO(vehicleCarDTO);

        String carType = vehicleCarDTO.getCarType();
        if (StringUtils.isNotEmpty(carType)) {
            queryVehicleCarBO.setCarType(VehicleCarTypeEnum.toVehicleCarTypeEnum(carType).getId());
        }

        return queryVehicleCarBO;
    }
    //*********Query BO <- Controller DTO*********


    //*********Create BO <- Controller Create DTO*********
    default CreateVehicleCarBO toCreateVehicleCarBO(VehicleCarCreateDTO vehicleCarCreateDTO, Integer id, String name) {
        CreateVehicleCarBO createVehicleCarBO = vehicleCarCreateDTO2CreateVehicleCarBO(vehicleCarCreateDTO, id, name);
        createVehicleCarBO.setCarType(VehicleCarTypeEnum.toVehicleCarTypeEnum(vehicleCarCreateDTO.getCarType()).getId());
        createVehicleCarBO.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnum(vehicleCarCreateDTO.getCardType()).getId());
        return createVehicleCarBO;
    }
    @Mapping(target = "creatorAccountId", source = "id")
    @Mapping(target = "creatorAccountName", source = "name")
    @Mapping(target = "carType", ignore = true)
    @Mapping(target = "cardType", ignore = true)
    CreateVehicleCarBO vehicleCarCreateDTO2CreateVehicleCarBO(VehicleCarCreateDTO vehicleCarCreateDTO, Integer id, String name);
    //*********Create BO <- Controller Create DTO*********
    //*********Entity <- Create BO*********
    VehicleCar toVehicleCar(CreateVehicleCarBO createVehicleCarBO, String projectName, Integer parkingLotId);
    //*********Entity <- Create BO*********


    //*********Delete BO <- Controller Delete DTO*********
    @Mapping(target = "deleterAccountId", source = "accountId")
    @Mapping(target = "deleterAccountName", source = "accountName")
    DeleteVehicleCarBO toDeleteVehicleCarBO(VehicleCarDeleteDTO vehicleCarDeleteDTO, Integer accountId, String accountName);
    //*********Delete BO <- Controller Delete DTO*********


    //*********Update BO <- Controller Update DTO*********
    default UpdateVehicleCarBO toUpdateVehicleCarBO(VehicleCarUpdateDTO vehicleCarUpdateDTO, Integer accountId, String accountName) {
        UpdateVehicleCarBO updateVehicleCarBO = vehicleCarUpdateDTO2UpdateVehicleCarBO(vehicleCarUpdateDTO, accountId, accountName);
        String carType = vehicleCarUpdateDTO.getCarType();
        if (StringUtils.isNotEmpty(carType)) {
            updateVehicleCarBO.setCarType(VehicleCarTypeEnum.toVehicleCarTypeEnum(carType).getId());
        }
        return updateVehicleCarBO;
    }
    @Mapping(target = "editorAccountId", source = "accountId")
    @Mapping(target = "editorAccountName", source = "accountName")
    @Mapping(target = "carType", ignore = true)
    UpdateVehicleCarBO vehicleCarUpdateDTO2UpdateVehicleCarBO(VehicleCarUpdateDTO vehicleCarUpdateDTO, Integer accountId, String accountName);
    //*********Update BO <- Controller Update DTO*********
    //*********Entity <- Update BO*********
    VehicleCar updateVehicleCarBO2VehicleCar(UpdateVehicleCarBO updateVehicleCarBO);
    //*********Entity <- Update BO*********


    //*********BO <- PO*********
    @IterableMapping(qualifiedByName = {"toVehicleCarBO"})
    List<VehicleCarBO> vehicleCarPOList2VehicleCarBOList(List<VehicleCarPO> vehicleCarPOList);
    @Named("toVehicleCarBO")
    default VehicleCarBO toVehicleCarBO(VehicleCarPO vehicleCarPO) {
        VehicleCarBO vehicleCarBO = vehicleCarPO2VehicleCarBO(vehicleCarPO);

        Integer carType = vehicleCarPO.getCarType();
        if (Objects.nonNull(carType)) {
            vehicleCarBO.setCarType(VehicleCarTypeEnum.toVehicleCarTypeEnum(carType).getDescription());
        }

        Integer cardType = vehicleCarPO.getCardType();
        if (Objects.nonNull(cardType)) {
            vehicleCarBO.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnum(cardType).getDescription());
        }

        return vehicleCarBO;
    }
    @Mapping(target = "vehicleCardBO", source = "vehicleCardPO", qualifiedByName = {"toVehicleCardBO"}, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    @Mapping(target = "vehicleRelationCarToCardBO", source = "vehicleRelationCarToCardPO", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    @Mapping(target = "carType", ignore = true)
    @Mapping(target = "cardType", ignore = true)
    VehicleCarBO vehicleCarPO2VehicleCarBO(VehicleCarPO vehicleCarPO);

    @Named("toVehicleCardBO")
    default VehicleCardBO toVehicleCardBO(VehicleCardPO vehicleCardPO) {
        VehicleCardBO vehicleCardBO = vehicleCardPO2VehicleCardBO(vehicleCardPO);
        vehicleCardBO.setCardType(VehicleCardTypeEnum.toVehicleCardTypeEnum(vehicleCardPO.getCardType()).getDescription());
        return vehicleCardBO;
    }
    @Mapping(target = "cardType", ignore = true)
    VehicleCardBO vehicleCardPO2VehicleCardBO(VehicleCardPO vehicleCardPO);

    VehicleRelationCarToCardBO vehicleRelationCarToCardPO2VehicleRelationCarToCardBO(VehicleRelationCarToCardPO vehicleRelationCarToCardPO);
    //*********BO <- PO*********


    //*********VO <- BO*********
    List<VehicleCarVO> vehicleCarBOList2VehicleCarVOList(List<VehicleCarBO> vehicleCarBOList);
    @Mapping(target = "vehicleCardVO", source = "vehicleCardBO", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    @Mapping(target = "vehicleRelationCarToCardVO", source = "vehicleRelationCarToCardBO", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
    VehicleCarVO vehicleCarBO2VehicleCarVO(VehicleCarBO vehicleCarBO);

    VehicleCardVO vehicleCardBO2VehicleCardVO(VehicleCardBO vehicleCardBO);

    VehicleRelationCarToCardVO vehicleRelationCarToCardBO2VehicleRelationCarToCardVO(VehicleRelationCarToCardBO vehicleRelationCarToCardBO);
    //*********VO <- BO*********

}
